package com.toolsder.demo.spring.security.jwt.entity;

import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * created by 猴子请来的逗比 On 2020/8/27
 *
 * @author by 猴子请来的逗比
 */
@Slf4j
@Data
@ToString
@NoArgsConstructor
public class JsonResult<T> implements Serializable {


    private static final long serialVersionUID = -883998965777244404L;

    /**
     * 状态码
     */
    private int code;

    /**
     * 提示信息
     */
    private String message;

    /**
     * 内容
     */
    private T result;

    /**
     * 服务器当前时间戳
     */
    private String timestamp = LocalDateTime.now().toString();

    public JsonResult(int code, String message, T result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }

    public JsonResult<T> success(T t) {
        return new JsonResult<>(Code.SUCCESS_CODE.code, "", t);
    }

    public static JsonResult<String> success() {
        return new JsonResult<>(Code.SUCCESS_CODE.code, "", "");
    }

    public static JsonResult<String> success(String result) {
        return new JsonResult<>(Code.SUCCESS_CODE.code, "", result);
    }

//    public static JsonResult<Object> success(Object object) {
//        return new JsonResult<>(Code.SUCCESS_CODE.code, "", object);
//    }

    public static JsonResult<String> error(String message) {
        return new JsonResult<>(Code.ERROR_CODE.code, message, "");
    }
    public static JsonResult<String> requestError(String message) {
        return new JsonResult<>(Code.REQUEST_ERROR_CODE.code, message, "");
    }



    public static JsonResult<String> forbidden(String message) {
        return new JsonResult<>(Code.FORBIDDEN_CODE.code, message, "");
    }

    public static JsonResult<String> forbidden() {
        return new JsonResult<>(Code.FORBIDDEN_CODE.code, "Forbidden", "");
    }


    @AllArgsConstructor
    @Getter
    public enum Code {
        SUCCESS_CODE(200),
        ERROR_CODE(201),
        REQUEST_ERROR_CODE(400),
        FORBIDDEN_CODE(403);
        private final int code;
    }
}
